package tw.twandroidproj.app.ui.SelectTime;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import tw.twandroidproj.app.R;

import static tw.twandroidproj.app.R.drawable.griditem;


public class gridAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    public  List<String> gridDataList = new ArrayList<>();
    private int clickTemp = -1;//标识被选择的item
    public  int[] clickedList=new int[16];
    private  Context c;
    public Drawable resources;

    private String selectedTime="";

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    gridAdapter(Context context){
        c = context;
        resources = c.getResources().getDrawable(R.drawable.griditem,null);
        layoutInflater = LayoutInflater.from(context);
        for (int i =0;i<16;i++){
            gridDataList.add(i+8+":00 ");
            clickedList[i]=0;
        }
    }

    public void setSeclection(int posiTion) {
        clickTemp = posiTion;
        }

    public String getTime(){

        return selectedTime;
    }

    public int[] getClickedList(){
        return this.clickedList;
    }

    public List<String> getGridDataList(){
        return this.gridDataList;
    }

    @Override
    public int getCount() {
        return gridDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return gridDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        gridHolder gridholder;
        if(convertView == null) {
            gridholder = new gridHolder();
            convertView = layoutInflater.inflate(R.layout.timeselect_griditem,null);
            gridholder.gridText = (TextView)convertView.findViewById(R.id.gridText);
            convertView.setTag(gridholder);
        }
        else {
            gridholder = (gridHolder) convertView.getTag();
        }

        String itemText = gridDataList.get(position);
        gridholder.gridText.setText(itemText);

        if(clickTemp==position){
            if (clickedList[position]==0){
                convertView.setBackgroundColor(Color.parseColor("#FFD2D1D2"));
                clickedList[position]=1;
                selectedTime += gridDataList.get(position);
                //Toast.makeText(c,selectedTime,Toast.LENGTH_SHORT).show();

            }
            else {
                convertView.setBackground(resources);
                //convertView.setBackgroundColor(Color.TRANSPARENT);
                clickedList[position]=0;
                selectedTime= selectedTime.replace(gridDataList.get(position),"");
            }
        }

        return convertView;
    }

    class gridHolder{
        public TextView gridText;
    }
}
